package csu.fun.spring_store.persistence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import csu.fun.spring_store.domain.Admin;
import org.springframework.stereotype.Repository;

@Repository
public interface AdminMapper extends BaseMapper<Admin> {

    //判断密码是否正确
    default boolean isPasswordCorrect(String adminName, String password) {
        Admin admin = this.selectById(adminName);
        if (admin != null) {
            return admin.getPassword().equals(password);
        }
        return false;
    }

    //返回指定id的密码
    default String getPasswordByAdminName(String adminName) {
        Admin admin = this.selectById(adminName);
        if (admin != null) {
            return admin.getPassword();
        }
        return null;
    }

    //插入一个新signon记录，返回实际操作的行数。admin不为null且adminname必须不在admin表中存在，否则不进行任何修改
    default int setNewAdmin(Admin admin) {
        if (admin == null) {
            return 0;
        }
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adminname", admin.getAdminName());
        if (!this.exists(queryWrapper)) {
            return this.insert(admin);
        }
        return 0;
    }

    //根据adminName和password新建一个admin记录，返回实际操作的行数。adminName不能为空，否则不进行任何修改
    default int setNewAdminByAdminNameAndPassword(String adminName, String password) {
        if (adminName == null || adminName.equals("") || adminName.isEmpty()) {
            return 0;
        }
        Admin admin = new Admin();
        admin.setAdminName(adminName);
        admin.setPassword(password);
        return this.setNewAdmin(admin);
    }

    //修改指定管理员的密码，返回实际修改的行数。adminName，否则不进行任何修改
    default int setPasswordByUsername(String adminName, String password) {
        Admin admin = this.selectById(adminName);
        if (admin != null) {
            admin.setPassword(password);
            return this.updateById(admin);
        }
        return 0;
    }

    //删除指定用户信息，返回实际修改的行数
    default int removeAdminByAdminName(String adminName) {
        return this.deleteById(adminName);
    }
}
